package vitaliy.bondar.diamondcave;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.opengl.util.GLState;

import vitaliy.bondar.diamondcave.SceneManager.SceneType;

public class LevelCompleteScene extends BaseScene implements IOnMenuItemClickListener{

    private MenuScene menuChildScene;
    private final int MENU_BACK = 0;
    private final int MENU_NEXT = 1;
    
    @Override
    public void createScene() {
        createBackground();
        createMenuChildScene();
        createLabel();
    }

    @Override
    public void onBackKeyPressed() {
    	SceneManager.getInstance().loadSelectorScene(engine);
    }

    @Override
    public SceneType getSceneType()
    {
        return SceneType.SCENE_LEVEL_COMPLETE;
    }

    @Override
    public void disposeScene() 
    {
        activity.runOnUpdateThread(new Runnable()
        {
            @Override
            public void run()
            {
                setChildrenIgnoreUpdate(true);
                setIgnoreUpdate(true);
                detachChildren();
            }
        });
    }
    
    private void createBackground()
    {
        attachChild(new Sprite(75, 40, resourcesManager.message_region, vbom)
        {
            @Override
            protected void preDraw(GLState pGLState, Camera pCamera)
            {
                super.preDraw(pGLState, pCamera);
                pGLState.enableDither();
            }
        });
    }
    
    private void createMenuChildScene()
    {
        menuChildScene = new MenuScene(camera);
        
        final IMenuItem backMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_BACK, resourcesManager.button_back_region, vbom), 1.2f, 1);
        final IMenuItem replayMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_NEXT, resourcesManager.button_next_region, vbom), 1.2f, 1);
        
        menuChildScene.addMenuItem(backMenuItem);
        menuChildScene.addMenuItem(replayMenuItem);
        
        menuChildScene.buildAnimations();
        menuChildScene.setBackgroundEnabled(false);
        
        replayMenuItem.setPosition( 250, 250 );
        backMenuItem.setPosition( 420, 250 );
        
        menuChildScene.setOnMenuItemClickListener(this);
        setChildScene(menuChildScene);
    }
    
    private void createLabel()
    {
        Text label = new Text(0, 130, resourcesManager.font, activity.getString(R.string.level_complete), vbom);
        Text shadow = new Text(0, 135, resourcesManager.font, activity.getString(R.string.level_complete), vbom);
        label.setColor(1f, 0.6f, 0f);
        shadow.setColor(0f, 0f, 0f);
        label.setScale(1.2f);
        shadow.setScale(1.2f);
        label.setPosition(camera.getCenterX() - label.getWidth() / 2, label.getY());
        shadow.setPosition(label.getX() + 5, label.getY() + 5);
        attachChild(shadow);
        attachChild(label);
    }
    
    public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem, float pMenuItemLocalX, float pMenuItemLocalY)
    {
            switch(pMenuItem.getID())
            {
            case MENU_BACK:
            	SceneManager.getInstance().loadSelectorScene( engine );
                return true;
            case MENU_NEXT:
            	SceneManager.getInstance().loadNextLevel( engine );
                return true;
            default:
                return false;
        }
    }
}
